const app = getApp()

Page({

  data: {
    errors: 'dsfsd',
    iconMap: {
      1: '/assets/svg/yongyinshenqing.svg',
      2: '/assets/svg/baoxiao.svg',
      3: '/assets/svg/fukuanshenqing.svg',
      4: '/assets/svg/wupinshengou.svg',
      5: '/assets/svg/hetong.svg'

    },
    yearList: [2023],
    monthList: [1,2,3,4,5,6,7,8,9,10,11,12],
    
    drawerShow: false,
    list: [],

    page: 1,
    pageSize: 20,
    finished: false, // 是否已加载完成，加载完成后不再触发数据加载
    // 选择用户
    selectUserShow: false,
    userList: [{"id":1,"name":"章建"},{"id":2,"name":"张凌燕"},{"id":3,"name":"施彦勤"},{"id":4,"name":"张天义"},{"id":5,"name":"董萍"},{"id":6,"name":"李怀健"},{"id":7,"name":"王彩云"},{"id":8,"name":"周瑾"},{"id":9,"name":"曾燃"},{"id":10,"name":"杨利泽"},{"id":11,"name":"张一笑"},{"id":12,"name":"陈鹤英"},{"id":13,"name":"岑效"},{"id":14,"name":"何佳婷"},{"id":15,"name":"陈德会"},{"id":16,"name":"蒋祺"},{"id":17,"name":"陈燕婷"},{"id":18,"name":"舒文琳"},{"id":19,"name":"陈凌泽"},{"id":20,"name":"张军龙"},{"id":21,"name":"张玲燕"},{"id":22,"name":"叶燕红"},{"id":23,"name":"赵冬霞"},{"id":24,"name":"邬政华"},{"id":25,"name":"陈伟"},{"id":26,"name":"郭芬"},{"id":27,"name":"戈良杰"}],
    chooseUser: null,  // 选中的用户

    // 选择部门
    selectDepartmentShow: false,
    departmentList: [],
    chooseDepartment: null,

    // 选择AO表单分类
    selectProcessShow: false,
    processList: [],
    chooseProcess: null,

    // 日期选择
    dateDrawerShow: false,
    chooseDate: null,
    pickerValue: [0,0]
  },
  tabDocument(event) {
    const data = this.data.list[event.target.dataset.index]
    console.log(data)
    this.setData({
      drawerShow: true
    })
  },
  onVisibleChangeDrawer () {
    this.setData({
      drawerShow: false
    });
  },
  // 测试钉盘预览
  // onTest () {
  //   console.log('dfsdf')
  //   dd.previewFileInDingTalk({
  //     corpId: 'dingf3b170a7280901ae35c2f4657eb6378f',
  //     fileId: '125712574222',
  //     spaceId: '314233097',
  //     fileName: '业委会换届咨询及会务服务合同（中山花苑）2023.11.pdf',
  //     fileSize: '247194',
  //     fileType: 'pdf',
  //     success: () => {},
  //     fail: () => {},
  //     complete: () => {},
  //   });
  // },
  // 用户 抽屉打开
  openUserListDrawer(){
    console.log('dfsf')
    // app.https('GET','/api/DingIndex/userList').then(r => {
    //   console.log('sdfsd',r)
    //   this.setData({
    //     userList: r.data
    //   })
    // })
    this.setData({
      selectUserShow: true
    })
  },
  // 用户 抽屉关闭
  onVisibleChangeUser(visible) {
    this.setData({
      selectUserShow: visible
    })
  },
  // 用户 选中
  tapSelectUser (event) {
    const d = this.data.userList[event.currentTarget.dataset.index]
    if (this.data.chooseUser && this.data.chooseUser.id === d.id) {
      this.setData({
        chooseUser: null,
        selectUserShow: false
      })
    } else {
      this.setData({
        chooseUser: d,
        selectUserShow: false
      })
    }
    // this.setData({
    //   chooseUser: this.data.userList[event.currentTarget.dataset.index],
    //   selectUserShow: false
    // })

    this.folderLoad()
    // chooseUser
  },

  // 部门 抽屉打开
  openDepartmentListDrawer(){
    app.https('GET','/api/DingIndex/departmentList').then(r => {
      const d = r.data.map(item => {
        return item.department
      })
      console.log('d', d)
      this.setData({
        departmentList: d
      })
    })
    this.setData({
      selectDepartmentShow: true
    })
  },
  // 部门 抽屉关闭
  onVisibleChangeDepartment(visible) {
    this.setData({
      selectDepartmentShow: visible
    })
  },
  
  // 部门 选中
  tapSelectDepartment (event) {


    const d = this.data.departmentList[event.currentTarget.dataset.index]
    if (this.data.chooseDepartment && this.data.chooseDepartment.id === d.id) {
      this.setData({
        chooseDepartment: null,
        selectDepartmentShow: false
      })
    } else {
      this.setData({
        chooseDepartment: d,
        selectDepartmentShow: false
      })
    }
    this.setData({
      list: [],
      page: 1,
      finished: false

    })
    this.folderLoad()
  },

  // AO表单分类 抽屉打开
  openProcessListDrawer(){
    console.log('dfsf')
    app.https('GET','/api/DingIndex/processList').then(r => {
      this.setData({
        processList: r.data
      })
    })
    this.setData({
      selectProcessShow: true
    })
  },
  // AO表单分类 抽屉关闭
  onVisibleChangeProcess(visible) {
    this.setData({
      selectProcessShow: visible
    })
  },
  // AO表单分类 选中
  tapSelectProcess (event) {
    // console.log(event.currentTarget.dataset.index)
    const d = this.data.processList[event.currentTarget.dataset.index]

    if (this.data.chooseProcess && this.data.chooseProcess.id === d.id) {
      this.setData({
        chooseProcess: null,
        selectProcessShow: false
      })
    } else {
      this.setData({
        chooseProcess: d,
        selectProcessShow: false
      })
    }
    this.setData({
      list: [],
      page: 1,
      finished: false
    })
    
    this.folderLoad()
  },





  // 数据加载
  folderLoad() {
    this.setData({
      errors: 'folderLoad_start_30'
    })
    const params = {
      page: this.data.page,
      pageSize: this.data.pageSize
    }
    // if (this.data.chooseUser) {
    //   params.userId = this.data.chooseUser.id
    // }

    if (this.data.chooseDepartment) {
      params.departmentId = this.data.chooseDepartment.id
    }

    if (this.data.chooseProcess) {
      params.processId = this.data.chooseProcess.id
    }

    if (this.data.chooseDate) {
      params.year = this.data.chooseDate.year
      params.month = this.data.chooseDate.month
    }

    this.setData({
      errors: 'folderLoad_start_https_32'
    })

    app.https('GET','/api/DingIndex/index',params).then(r => {
      console.log('R', r)

      if (r.data.length < this.data.pageSize) {
        // 加载完成
        console.log('ok')
        this.setData({
          finished: true
        })
      }

      const tempArr = this.data.list
      tempArr.push(...r.data)
      this.setData({
        list: tempArr,
        page: this.data.page + 1
      })
    })



  },

  // 查看附件内容
  tapAttachments(event){
    // console.log(event.currentTarget.dataset.id)
    dd.navigateTo({
      url: '/pages/attachments/attachments?id='+event.currentTarget.dataset.id,
      success: () => {},
      fail: () => {},
      complete: () => {},
    });
  },

  // 抽屉 打开日期选择
  openDateDrawer() {
    this.setData({
      dateDrawerShow: true
    })
  },
  // 抽屉 关闭日期选择
  onVisibleChangeDate (visible) {
    this.setData({
      dateDrawerShow: visible
    })
  },

  onChangePicker(e) {
    console.log(e.detail.value);
    this.setData({
      pickerValue: e.detail.value,
    });
  },

  // 日期清除
  dateClearTab() {
    this.setData({
      chooseDate: null,
      dateDrawerShow:false,
      list:[],
      page: 1,
      finished: false
    })
    this.folderLoad()

  },

  // 确认日期
  dateChooseTab () {

    const year = this.data.yearList[this.data.pickerValue[0]]
    const month = this.data.monthList[this.data.pickerValue[1]]

    if (this.data.chooseDate===null || this.data.chooseDate.year !== year || this.data.chooseDate.month !== month) {
      this.setData({
        chooseDate: { year, month },
        dateDrawerShow: false,
        page: 1,
        list: [],
        finished: false
      })
      this.folderLoad()
    }
    // const obj = {
    //   year: this.data.yearList[this.data.pickerValue[0]],
    //   month: this.data.monthList[this.data.pickerValue[1]]
    // }
    // this.setData({
    //   chooseDate: obj,
    //   dateDrawerShow: false
    // })

    
  },


  onLoad(query) {
    this.setData({
      errors: 'start'
    })

    this.folderLoad()

    // 页面加载
    // console.info(`Page onLoad with query: ${JSON.stringify(query)}`);
    // dd.getAuthCode({
    //   success:function(res){
    //     console.log(res)

    //     app.https('GET','/api/Auth/login',res).then(r => {
    //       console.log('res', r)
    //     })
    //       /*{
    //           authCode: 'hYLK98jkf0m' //string authCode
    //       }*/
    //   },
    //   fail:function(err){
    //     console.log(err)
      // }
  // });
    
  },
  onReady() {
    // 页面加载完成
    // dd.previewFileInDingTalk({
    //   corpId: 'dingf3b170a7280901ae35c2f4657eb6378f',
    //   fileId: '124653550994',
    //   spaceId: '275266943',
    //   fileName: 'wy_cs_job.xlsx',
    //   fileSize: '2794',
    //   fileType: 'xlsx',
    //   success: () => {},
    //   fail: () => {},
    //   complete: () => {},
    // });
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
    console.log('di')

    if (this.data.finished === false) {
      this.folderLoad()
    }
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
